//
//  ZDWelcomeViewController.m
//  OCSinaWeiBo
//
//  Created by Apple on 15/10/30.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDWelcomeViewController.h"
#import <Masonry/Masonry.h>
#import "ZDTabBarController.h"
#import "UIImageView+Extension.h"
#import "ZDUserAccount.h"


extern NSString *const ZDSwitchControllerNotification;

@interface ZDWelcomeViewController ()

@property (nonatomic, strong) UIImageView *backgroundImageView;

@property (nonatomic, strong) UIImageView *iconView;

@property (nonatomic, strong) UILabel *label;

@end

@implementation ZDWelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self prepareUI];
    
    NSURL *imageURL = [NSURL URLWithString:[ZDUserAccount loadUserAccount].avatar_large];
    
    //下载用户头像
    [self.iconView zd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
}


#pragma mark - view显示完毕后 开始头像动画
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self startIconViewAnimation];
}


#pragma mark - 准备UI
- (void)prepareUI
{
    //添加子控件
    [self.view addSubview:self.backgroundImageView];
    [self.view addSubview:self.iconView];
    [self.view addSubview:self.label];
    
    //添加约束
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.bottom.right.mas_equalTo(self.view);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-160);
        make.width.height.mas_equalTo(85);
    }];
    
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.iconView.mas_bottom).offset(10);
    }];
}

#pragma mark - 头像动画
- (void)startIconViewAnimation
{
    [UIView animateWithDuration:1.0 delay:0.1 usingSpringWithDamping:0.6 initialSpringVelocity:0.5 options:0 animations:^{
        
        //重新设置约束
        [self.iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.mas_equalTo(self.view.mas_centerX);
            make.bottom.mas_equalTo(self.view.mas_top).offset(160);
            make.width.height.mas_equalTo(85);
        }];
        
        //立刻重新布局
        [self.view layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:0.25 animations:^{
            
            self.label.alpha = 1.0;
            
        } completion:^(BOOL finished) {
            
            //动画执行完后，跳转到主控制器
            [[NSNotificationCenter defaultCenter] postNotificationName:ZDSwitchControllerNotification object:self userInfo:@{@"class":[ZDTabBarController class]}];
        }];
    }];
}


#pragma mark 懒加载
- (UIImageView *)backgroundImageView
{
    if (!_backgroundImageView)
    {
        _backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad_background"]];
    }
    
    return _backgroundImageView;
}

- (UIImageView *)iconView
{
    if (!_iconView)
    {
        _iconView = [[UIImageView alloc] init];
        
        UIImage *image = [UIImage imageNamed:@"avatar_default_big"];
        _iconView.image = image;
        
        _iconView.layer.cornerRadius = MIN(image.size.width, image.size.height) * 0.5;   //根据图片尺寸计算
        _iconView.layer.masksToBounds = YES;
    }
    
    return _iconView;
}

- (UILabel *)label
{
    if (!_label)
    {
        _label = [[UILabel alloc] init];
        
        _label.text = @"大圣归来";
        
        _label.alpha = 0;
    }
    
    return _label;
}


@end
